<?php

/**
 * Created by PhpStorm.
 * User: chen
 * Date: 2015/5/7
 * Time: 13:32
 */
class WxuserModel extends Model
{
    /**
     * 根据OPENID 获取用户信息
     * @param $openid
     * @return array
     */
    public function findByOpenId($openid)
    {
        $map = array();
        $map['openid'] = $openid;
        return $this->where($map)->find();
    }

    /**
     * 获取微信用户信息，添加到表中
     */
    public function addWxInfo($user)
    {
        $data = array();
        $data['wecha_id'] = $user['wecha_id'];
        $data['openid'] = $user['openid'];
        $data['nickname'] = $user['nickname'];
        // $data['nickname1'] = $user['nickname1'];
        $data['imgurl'] = $user['imgurl'];
        $data['sex'] = $user['sex'];
        $data['province'] = $user['province'];
        $data['city'] = $user['city'];
        $data['country'] = $user['country'];
        $map = array();
        $map['openid'] = $data['openid'];
        $exist = $this->where($map)->find();
        if ($exist) {
            $method='save';
        } else {
             $this->data($data)->add();
            $method='add';
        }
        return $method;
    }

    /**
     * 根据openid获取用户id
     */
    public function getIdByOpenId($openid)
    {
        $map = array();
        $map['openid'] = $openid;
        return $this->where($map)->getField('id');
    }

    /**
     * 根据id获取用户openid
     */
    public function getOpenIdById($id)
    {
        $map = array();
        $map['id'] = $id;
        return $this->where($map)->getField('openid');
    }

    /**
     * 根据id添加玩游戏点击数
     */
    public function addClickCount($id, $count)
    {
        $map = array();
        $map['id'] = $id;
        // $oldPoint=$this->where($map)->getField('played');
        // if($oldPoint<$count){
            $data = array();
            $data['played'] = $count;
            $this->where($map)->data($data)->save();
        // }
    }
}